package com.stylefeng.guns.gateway.modular.film;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.rpc.RpcContext;
import com.stylefeng.guns.api.film.FilmAsyncServiceApi;
import com.stylefeng.guns.api.film.FilmServiceApi;
import com.stylefeng.guns.api.film.vo.*;
import com.stylefeng.guns.gateway.modular.film.vo.FilmConditionVO;
import com.stylefeng.guns.gateway.modular.film.vo.FilmIndexVO;
import com.stylefeng.guns.gateway.modular.film.vo.FilmRequestVO;
import com.stylefeng.guns.gateway.modular.vo.ResponseVO;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RestController
@RequestMapping("/film/")
public class FilmController {

    public static final String IMG_PRE = "http://img.meetingshop.cn";

    @Reference(interfaceClass = FilmServiceApi.class, check = false)
    private FilmServiceApi filmServiceApi;

    @Reference(interfaceClass = FilmAsyncServiceApi.class, async = true, check = false)
    private FilmAsyncServiceApi filmAsyncServiceApi;

    /**
     * 获取首页接口信息
     *
     * API网关:
     *  1.接口功能聚合(API聚合)
     *      好处: 1.六个接口，一次请求，同一时间节省了五次HTTP请求
     *           2.同一接口对外暴露，降低了前后端分离开发的难度和复杂度
     *      坏处: 一次获取过多数据，容易出现问题
     * @return ResponseVO
     */
    @RequestMapping(value = "getIndex", method = RequestMethod.GET)
    public ResponseVO getIndex(){
        FilmIndexVO filmIndexVO = new FilmIndexVO();

        // 获取banner信息
        filmIndexVO.setBanners(filmServiceApi.getBanners());
        // 获取热映的电影
        filmIndexVO.setHotFilms(filmServiceApi.getHotFilms(true, 8,1,1,99,99,99));
        // 获取即将上映的电影
        filmIndexVO.setSoonFilms(filmServiceApi.getSoonFilms(true, 8,1,1,99,99,99));
        // 票房排行榜
        filmIndexVO.setBoxRanking(filmServiceApi.getBoxRanking());
        // 获取受欢迎的榜单
        filmIndexVO.setExpectRanking(filmServiceApi.getExpectRanking());
        // 获取前一百
        filmIndexVO.setTop100(filmServiceApi.getTop());
        
        return ResponseVO.success(IMG_PRE,filmIndexVO);
    }

    @RequestMapping(value = "getConditionList", method = RequestMethod.GET)
    public ResponseVO getConditionList(@RequestParam(name = "catId", required = false, defaultValue = "99")String catId,
                                       @RequestParam(name = "sourceId", required = false, defaultValue = "99")String sourceId,
                                       @RequestParam(name = "yearId", required = false, defaultValue = "99")String yearId){
        FilmConditionVO filmConditionVO = new FilmConditionVO();

        // 标识位
        boolean flag = false;
        // 类型集合
        List<CatVO> cats = filmServiceApi.getCats();
        List<CatVO> catResult = new ArrayList<>();
        CatVO cat = null;
        for (CatVO catVO : cats){
            // 判断集合是否存在catId，如果存在，则将对应的实体变成active
            // 6
            // 1,2,3,99,4,5
                // 优化: 1.数据层查询按Id进行排序【有序集合->有序数组】
                      // 2.通过二分法查找
            if(catVO.getCatId().equals("99")){
                cat = catVO;
                continue;
            }
            if(catVO.getCatId().equals(catId)){
                flag = true;
                catVO.setActive(true);
            }else{
                catVO.setActive(false);
            }
            catResult.add(catVO);
        }
        // 如果不存在，则默认将全部变为Active状态
        if(!flag){
            cat.setActive(true);
            catResult.add(cat);
        }else{
            cat.setActive(false);
            catResult.add(cat);
        }

        // 片源集合
        flag = false;
        List<SourceVO> sources = filmServiceApi.getSources();
        List<SourceVO> sourceResult = new ArrayList<>();
        SourceVO source = null;
        for (SourceVO sourceVO : sources){
            // 判断集合是否存在catId，如果存在，则将对应的实体变成active
            // 6
            // 1,2,3,99,4,5
            // 优化: 1.数据层查询按Id进行排序【有序集合->有序数组】
            // 2.通过二分法查找
            if(sourceVO.getSourceId().equals("99")){
                source = sourceVO;
                continue;
            }
            if(sourceVO.getSourceId().equals(sourceId)){
                flag = true;
                sourceVO.setActive(true);
            }else{
                sourceVO.setActive(false);
            }
            sourceResult.add(sourceVO);
        }
        // 如果不存在，则默认将全部变为Active状态
        if(!flag){
            source.setActive(true);
            sourceResult.add(source);
        }else{
            source.setActive(false);
            sourceResult.add(source);
        }

        // 年代集合
        flag = false;
        List<YearVO> years = filmServiceApi.getYears();
        List<YearVO> yearResult = new ArrayList<>();
        YearVO year = null;
        for (YearVO yearVO : years){
            // 判断集合是否存在catId，如果存在，则将对应的实体变成active
            // 6
            // 1,2,3,99,4,5
            // 优化: 1.数据层查询按Id进行排序【有序集合->有序数组】
            // 2.通过二分法查找
            if(yearVO.getYearId().equals("99")){
                year = yearVO;
                continue;
            }
            if(yearVO.getYearId().equals(yearId)){
                flag = true;
                yearVO.setActive(true);
            }else{
                yearVO.setActive(false);
            }
            yearResult.add(yearVO);
        }
        // 如果不存在，则默认将全部变为Active状态
        if(!flag){
            year.setActive(true);
            yearResult.add(year);
        }else{
            year.setActive(false);
            yearResult.add(year);
        }

        filmConditionVO.setCatInfo(catResult);
        filmConditionVO.setSourceInfo(sourceResult);
        filmConditionVO.setYearInfo(yearResult);
        return ResponseVO.success(filmConditionVO);
    }

    /**
     *
     * @param filmRequestVO
     * @return
     */
    @RequestMapping(value = "getFilms", method = RequestMethod.GET)
    public ResponseVO getFilms(FilmRequestVO filmRequestVO){
        FilmVO filmVO = null;
        // 根据showTime判断影片查询类型
        switch (filmRequestVO.getShowType()){
            case 1:
                filmVO = filmServiceApi.getHotFilms(false, filmRequestVO.getPageSize(),
                        filmRequestVO.getNowPage(),filmRequestVO.getSortId(),
                        filmRequestVO.getSourceId(), filmRequestVO.getYearId(),
                        filmRequestVO.getCatId());
                break;
            case 2:
                filmVO = filmServiceApi.getSoonFilms(false, filmRequestVO.getPageSize(),
                        filmRequestVO.getNowPage(),filmRequestVO.getSortId(),
                        filmRequestVO.getSourceId(), filmRequestVO.getYearId(),
                        filmRequestVO.getCatId());
                break;
            case 3:
                filmVO = filmServiceApi.getHotFilms(false, filmRequestVO.getPageSize(),
                        filmRequestVO.getNowPage(),filmRequestVO.getSortId(),
                        filmRequestVO.getSourceId(), filmRequestVO.getYearId(),
                        filmRequestVO.getCatId());
                break;
            default:
                filmVO = filmServiceApi.getClassicFilms(filmRequestVO.getPageSize(),
                        filmRequestVO.getNowPage(),filmRequestVO.getSortId(),
                        filmRequestVO.getSourceId(), filmRequestVO.getYearId(),
                        filmRequestVO.getCatId());
                break;


        }
        // 根据sortId排序
        // 添加各种条件查询
        // 判断当前是第几页
        return ResponseVO.success(filmVO.getNowPage(), filmVO.getTotalPage(),IMG_PRE, filmVO.getFilmInfo());
    }

    @RequestMapping(value = "films/{searchParams}",method = RequestMethod.GET)
    public ResponseVO films(@PathVariable("searchParams")String searchParams, int searchType) throws ExecutionException, InterruptedException {
        // 根据serachType，判断查询类型
        FilmDetalVO filmDetalVO = filmServiceApi.getFilmDetal(searchType, searchParams);

        if(filmDetalVO == null){
            return ResponseVO.serviceFail("没有可查询的影片");
        }else if(filmDetalVO.getFilmId() == null || filmDetalVO.getFilmId().trim().length() == 0){
            return ResponseVO.serviceFail("没有可查询的影片");
        }
        String filmId = filmDetalVO.getFilmId();
        // 查询影片的详细信息 ->dubbo异步获取
        //获取影片描述信息
//        FilmDescVO filmDescVO = filmAsyncServiceApi.getFilmDesc(filmId);
        filmAsyncServiceApi.getFilmDesc(filmId);
        Future<FilmDescVO> filmDescVOFuture = RpcContext.getContext().getFuture();
        // 获取图片信息
//        ImgVO imgVO = filmAsyncServiceApi.getImgs(filmId);
        filmAsyncServiceApi.getImgs(filmId);
        Future<ImgVO> imgVOFuture = RpcContext.getContext().getFuture();
        // 获取导演信息
//        ActorVO directorVO = filmAsyncServiceApi.getDectInfo(filmId);
        filmAsyncServiceApi.getDectInfo(filmId);
        Future<ActorVO> directorVOFuture = RpcContext.getContext().getFuture();
        // 获取演员信息
//        List<ActorVO> actors = filmAsyncServiceApi.getActors(filmId);
        filmAsyncServiceApi.getActors(filmId);
        Future<List<ActorVO>> actorsFuture = RpcContext.getContext().getFuture();
        InfoRequestVO infoRequestVO = new InfoRequestVO();
        ActorRequestVO actorRequestVO = new ActorRequestVO();
        actorRequestVO.setActors(actorsFuture.get());
        actorRequestVO.setDirector(directorVOFuture.get());

        infoRequestVO.setActorRequestVO(actorRequestVO);
        infoRequestVO.setBiography(filmDescVOFuture.get().getBiography());
        infoRequestVO.setFilmId(filmId);
        infoRequestVO.setImgVO(imgVOFuture.get());

        // 组织成返回值
        filmDetalVO.setInfo4(infoRequestVO);
        return ResponseVO.success(IMG_PRE, filmDetalVO);
    }
}
